package org.cargloss.cp.controller.modul.event;

import java.util.List;

import org.cargloss.cp.model.Event;

import util.hibernate.DAO;

import com.opensymphony.xwork2.ActionSupport;

public class ActionList extends ActionSupport {
	/**
         *
         */
	private static final long serialVersionUID = 4151236302794994027L;

	private List<Event> list;
	private Integer rows;
	private Integer page;
	private String sord;
	private String sidx;
	private String searchField;
	private String searchString;
	private String searchOper;
	private String _search;
	private Integer total;
	private long records;

	public String get_search() {
		return _search;
	}

	public void set_search(String _search) {
		this._search = _search;
	}

	public String getSidx() {
		return sidx;
	}

	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public String getSord() {
		return sord;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}

	public String getSearchField() {
		return searchField;
	}

	public void setSearchField(String searchField) {
		this.searchField = searchField;
	}

	public String getSearchString() {
		return searchString;
	}

	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}

	public String getSearchOper() {
		return searchOper;
	}

	public void setSearchOper(String searchOper) {
		this.searchOper = searchOper;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public long getRecords() {
		return records;
	}

	public void setRecords(Integer records) {
		this.records = records;
	}

	@SuppressWarnings("unchecked")
	public String execute() {

		int to = (rows * page);
		int from = to - rows;

		DAO<Integer> dao = new DAO<Integer>();
		records = dao.count(Event.class);
		dao.createCriteria(Event.class);
		// dao.createAliasLeftJoin("aksesKelompokMenu", "akm");
		if (!get_search().equals("true")) {
			if (getSidx().isEmpty())
				list = (List<Event>) dao.find(from, to);
			else {
				dao.addOrder(getSord(), getSidx());
				list = (List<Event>) dao.find(from, to);
			}
			// calculate the total pages for the query
		} else {
			if (getSidx().isEmpty()) {
				dao.addFilter(getSearchOper(), getSearchField(),
						getSearchString());
				list = (List<Event>) dao.find(from, to);
			} else {
				dao.addFilter(getSearchOper(), getSearchField(),
						getSearchString());
				dao.addOrder(getSord(), getSidx());
				list = (List<Event>) dao.find(from, to);
			}
		}
		dao.close();
		total = (int) Math.ceil((double) records / (double) rows);

		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String searchresult() {
		DAO<Integer> dao = new DAO<Integer>();
		dao.createCriteria(Event.class);
		// dao.createAlias("Event", "p");
		dao.searchFilter("like", searchField, searchString);
		// dao.addOrder(getSord(), getSidx());
		list = (List<Event>) dao.find();
		records = dao.count(Event.class);
		dao.close();

		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String paging() {
		DAO<Integer> dao = new DAO<Integer>();
		records = dao.count(Event.class);
		dao.createCriteria(Event.class);
		int to = rows * page;
		int from = (to - rows);
		to = rows;
		dao.addOrder("asc", "id");
		System.out.println(from + " " + to);
		list = (List<Event>) dao.find(from, rows);
		total = (int) Math.ceil((double) records / (double) rows);
		dao.close();

		return SUCCESS;
	}

	public String page() {
		return execute();
	}

	public String jqgrid() {
		return "json";
	}

	public List<Event> getList() {
		return list;
	}

	public void setList(List<Event> list) {
		this.list = list;
	}

	public void setRecords(long records) {
		this.records = records;
	}

}
